﻿Public Class HDCurveForm

    Private _g As Graphics
    Private _gPrint As Graphics

    Private _bmp As Bitmap
    Private _bmpPrint As Bitmap

    Private _bgColor As Color = Color.Black
    Private _bgColor4Print As Color = Color.White

    Private _foreColor As Color = Color.White
    Private _foreColor4Print As Color = Color.Black

    Private _scaleColor As Color = Color.Gray

    Private _l As New List(Of RTDValueInfo)
    Private _dl As New List(Of RTData)

    Private Const SCALECOUNT As Integer = 5

    Dim aWidth As Integer = 720 '830 '880
    Dim aHeight As Integer = 500
    Dim aTop As Integer = 20
    Dim aLeft As Integer = 180 '120

    Dim hasTemp As Boolean
    Dim hasHumi As Boolean
    Dim isSelecting As Boolean = True

    Dim _forePen As New Pen(Color.White)
    Dim _forePen4Print As New Pen(Color.Black)

    Private _foreBrush As New SolidBrush(Color.White)
    Private _foreBrush4Print As New SolidBrush(Color.Black)

    '温湿度上下限
    Private MAX_TEMP_LIMIT As Integer = 50
    Private MIN_TEMP_LIMIT As Integer = 0
    Private MAX_HUM_LIMIT As Integer = 100
    Private MIN_HUM_LIMIT As Integer = 0

    '温湿度坐标区域
    Private _THC_Area As System.Drawing.Rectangle



    Private Sub InitBitmap()
        _bmp = New Bitmap(1024, 570, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
        Me.PictureBox1.Image = _bmp
        _g = Graphics.FromImage(_bmp)
        _g.Clear(Color.Black)

        _bmpPrint = New Bitmap(1024, 570, System.Drawing.Imaging.PixelFormat.Format32bppArgb)
        _gPrint = Graphics.FromImage(_bmpPrint)
        _gPrint.Clear(Color.White)
    End Sub

    Private Sub HDCurveForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.CenterToParent()
        Me.dtpTo.Value = Now.Date
        Me.dtpFrom.Value = Me.dtpTo.Value.AddDays(-1)

        InitBitmap()
        LoadWareHouse()
        'Me.WindowState = FormWindowState.Maximized

        'Me.btnRefresh.PerformClick()
        Me.labCur.BackColor = Color.FromArgb(65, 204, 212, 230)
    End Sub

    ''' <summary>
    ''' 加载仓库
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub LoadWareHouse()
        Dim rts As RefTypeService = New RefTypeService(MainForm.pi)

        For Each rti As RefTypeItem In rts.GeRefGroupList()
            cboWarehouse.Items.Add(rti)
        Next

        If cboWarehouse.Items.Count >= 1 Then
            cboWarehouse.SelectedIndex = 0
            btnRefresh.Enabled = True
            btnSavePic.Enabled = True
        Else
            cboWarehouse.SelectedIndex = -1
            btnRefresh.Enabled = False
            btnSavePic.Enabled = False
        End If

    End Sub

    Private Sub cboWarehouse_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboWarehouse.SelectedIndexChanged
        Dim portnum As Integer = 0
        hasTemp = False
        hasHumi = False
        isSelecting = True

        _l.Clear()

        Dim Color_Zone() As Color = {Color.DarkRed, Color.DarkGoldenrod, Color.DarkGreen, Color.DarkBlue, Color.DarkOrange, Color.DeepPink, Color.Red, Color.Goldenrod, Color.Green, Color.Blue, Color.Orange, Color.Pink}

        Dim cbo_rti As RefTypeItem = CType(cboWarehouse.SelectedItem, RefTypeItem)
        Dim apts As AiPortTypeService = New AiPortTypeService(cbo_rti)

        MAX_TEMP_LIMIT = cbo_rti.HDCurveTempUp
        MIN_TEMP_LIMIT = cbo_rti.HDCurveTempDown
        MAX_HUM_LIMIT = cbo_rti.HDCurveHumiUp
        MIN_HUM_LIMIT = cbo_rti.HDCurveHumiDown

        For Each apti As AiPortTypeItem In apts.GetAiGroupList()
            If String.IsNullOrEmpty(apti.PortName) = False AndAlso apti.PortNo <> 0 Then '如果AI名称不为空并且端口号不为0
                'portnum += 1
                If apti.DataType = 1 Then '温度
                    _l.Add(CreateRTDValueInfo(apti.PortNo, apti.PortName, MAX_TEMP_LIMIT, MIN_TEMP_LIMIT, Color_Zone(apti.PortNo - 1), apti.DataType))
                    If hasTemp = False Then
                        portnum += 1
                        hasTemp = True
                    End If
                ElseIf apti.DataType = 2 Then '湿度
                    _l.Add(CreateRTDValueInfo(apti.PortNo, apti.PortName, MAX_HUM_LIMIT, MIN_HUM_LIMIT, Color_Zone(apti.PortNo - 1), apti.DataType))
                    If hasHumi = False Then
                        portnum += 1
                        hasHumi = True
                    End If
                End If
            End If
        Next

        '根据需要显示的温湿度的数量调整曲线的坐标
        aWidth = 720 '830
        aHeight = 500
        aTop = 20
        aLeft = 40 * portnum + 20

        GetTHCArea()

        InitBG(_g) '刷新图层但不显示数据
        Me.PictureBox1.Image = _bmp

        isSelecting = False
    End Sub

    Private Function CreateRTDValueInfo(ByVal id As Integer, ByVal name As String, ByVal maxvalue As Integer, ByVal minvalue As Integer, ByVal c As Color, ByVal dataType As Integer) As RTDValueInfo
        Dim vi As New RTDValueInfo()
        vi.ID = id
        vi.Name = name
        vi.maxValue = maxvalue
        vi.minValue = minvalue
        vi.color = c
        vi.length = 2
        vi.dataType = dataType
        Return vi
    End Function

    'Private Function CreateRTDValueInfo(ByVal i As Integer) As RTDValueInfo
    '    Return CreateRTDValueInfo(i, Color.Red)
    'End Function

    'Private Function CreateRTDValueInfo(ByVal i As Integer, ByVal c As Color) As RTDValueInfo
    '    Dim strName As String = ""
    '    If i <= 16 Then
    '        If i <= 8 Then
    '            strName = "T" & i.ToString()
    '        Else
    '            strName = "H" & (i - 8).ToString()
    '        End If

    '        'If i Mod 2 = 1 Then
    '        '    strName = "温度" & ((i - 1) \ 2 + 1).ToString()
    '        'Else
    '        '    strName = "湿度" & ((i - 1) \ 2 + 1).ToString()
    '        'End If

    '    ElseIf i <= 25 Then
    '        strName = "TBCC_T" & (i - 16).ToString()

    '    ElseIf i <= 28 Then
    '        strName = "TBCC_H" & (i - 25).ToString()
    '    End If
    '    Return CreateRTDValueInfo(strName, 100, 0, c)
    'End Function

    'Private Function CreateRTDValueInfo(ByVal i As Integer, ByVal maxvalue As Integer, ByVal minvalue As Integer, ByVal c As Color) As RTDValueInfo
    '    Dim strName As String = ""
    '    If i <= 16 Then
    '        If i <= 8 Then
    '            strName = "T" & i.ToString()
    '        Else
    '            strName = "H" & (i - 8).ToString()
    '        End If
    '        'If i Mod 2 = 0 Then
    '        '    strName = "温度" & ((i - 1) \ 2 + 1).ToString()
    '        'Else
    '        '    strName = "湿度" & ((i - 1) \ 2 + 1).ToString()
    '        'End If
    '    ElseIf i <= 25 Then
    '        strName = "TBCC_T" & (i - 16).ToString()

    '    ElseIf i <= 28 Then
    '        strName = "TBCC_H" & (i - 25).ToString()
    '    End If


    '    Return CreateRTDValueInfo(i, strName, maxvalue, minvalue, c)

    'End Function

    Private Function IsValidated() As Boolean
        Dim dif As TimeSpan
        dif = dtpTo.Value - dtpFrom.Value
        If dif.TotalHours < 2 OrElse dif.TotalHours > 7 * 24 Then
            dtpTo.Focus()
            msg.ShowWarning("查询间隔在2小时至7天之间。")
            Return False
        End If

        Return True
    End Function

    Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
        If IsValidated() Then

            If Me.btnRefresh.Text.Equals("刷新") Then
                InitBG(_g) '刷新图层但不显示数据

                'RefreshData()
                Me.pnlSearch.Visible = True
                'Me.btnRefresh.Enabled = False
                Me.btnRefresh.Text = "取消"
                Me.cboWarehouse.Enabled = False
                Me.dtpFrom.Enabled = False
                Me.dtpTo.Enabled = False
                Me.btnSavePic.Enabled = False
                Me.btnTHCCfg.Enabled = False
                'Me.PictureBox1.Focus()
                bwSearch.RunWorkerAsync(New Object() {cboWarehouse.SelectedItem, dtpFrom.Value, dtpTo.Value})
                'RefreshImage()
            Else
                Me.btnRefresh.Enabled = False
                bwSearch.CancelAsync()
            End If

        End If
    End Sub

    ''' <summary>
    ''' 刷新
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub RefreshData(ByVal argument As Object)
        _dl.Clear()

        Dim strSql As String
        Dim cbo_rti As RefTypeItem = argument(0) 'CType(cboWarehouse.SelectedItem, RefTypeItem)
        Dim dtF As DateTime = argument(1)
        Dim dtT As DateTime = argument(2)

        '{2}%00
        strSql = String.Format("Select * From tbccHDData_{0}_{1} where hDate >= '{2}' and hDate <= '{3}' Order by hdate", _
            cbo_rti.ProjectID.Replace("-", "_"), _
            cbo_rti.NetId, _
            UtilityClass.GetDateString(dtF), _
            UtilityClass.GetDateString(dtT) _
        )

        Dim ds As DataSet
        ds = SqlHelper.ExecuteDataset(strSql, Nothing)

        Dim count As Integer
        count = ds.Tables(0).Rows.Count

        Dim dt As DateTime
        Dim im As Decimal

        im = ((dtT - dtF).TotalSeconds / 10) / 720

        'If (dtT - dtF).Days >= 1 Then
        '    type = 1
        '    im = ((((dtT - dtF).Days * 24 + (dtT.Hour - dtF.Hour)) * 60 + (dtT.Minute - dtF.Minute)) * 6 + (dtT.Second - dtF.Second) / 10) / 720
        'Else
        '    type = 2
        '    im = (((dtT.Hour - dtF.Hour) * 60 + (dtT.Minute - dtF.Minute)) * 6 + (dtT.Second - dtF.Second) / 10) / 720
        '    'im = Math.Ceiling(im)
        'End If

        For Each vi As RTDValueInfo In _l
            Dim v(24 * 60) As Single
            For j As Integer = 0 To v.Length - 1
                v(j) = Single.NaN
            Next

            Dim proIndex As Integer = 0
            Dim i As Integer = 0
            For Each r As DataRow In ds.Tables(0).Rows
                If Me.bwSearch.CancellationPending Then '取消查询
                    ds.Clear() : ds.Dispose() : ds = Nothing
                    Exit Sub
                End If
                dt = r("hDate")
                'If dt.Second <> 0 Then '非整分钟
                '    Continue For
                'End If

                i = (dt - dtF).TotalSeconds / 10

                If Math.Round(i Mod im) <> 0 Then
                    Continue For
                End If

                i = i / im

                v(i) = r(String.Format("AI{0}", vi.ID.ToString()))
                proIndex += 1
                ReportProgress((proIndex / ds.Tables(0).Rows.Count) * 100) '进度报告
            Next

            Dim item As New RTData()
            item._bufSingle = v
            item.rtdItem = vi

            _dl.Add(item)
        Next


        'For Each vi As RTDValueInfo In _l

        '    Dim v(86400 / 120) As Single
        '    For i As Integer = 0 To v.Length - 1
        '        v(i) = g_rand.Next(30, 70)
        '    Next

        '    Dim item As New RTData()
        '    item._bufSingle = v
        '    item.rtdItem = vi

        '    _dl.Add(item)
        'Next

    End Sub


    Private Sub RefreshImage()
        'InitBG(_g)
        DrawLine(_g)
        Me.PictureBox1.Image = _bmp
    End Sub

    Private Sub RefreshImage4Print()
        InitBG4Print(_gPrint)
        DrawLine4Print(_gPrint)
        'Me.PictureBox1.Image = _bmpPrint
    End Sub

    ''' <summary>
    ''' 初始化背景
    ''' </summary>
    ''' <param name="g"></param>
    ''' <remarks></remarks>
    Private Sub InitBG(ByVal g As Graphics)
        Me.labLine.Visible = False
        Me.labInfo.Visible = False
        g.Clear(_bgColor)
        DrawMark(g)
        DrawCoordinates(g)
        DrawXCoordinate(g, False)
        DrawYCoordinate(g)
        SetCurLabPara()
        SetInfoLabPara()
    End Sub



    Private Sub DrawMark(ByVal g As Graphics)
        Dim markTop As Integer = 25
        Dim height As Integer = 25
        Dim left As Integer = aLeft + aWidth + 40

        Dim rMark As New Rectangle(left, aTop, 120, height * _l.Count)
        g.DrawRectangle(_forePen, rMark)

        Dim font As Font = New Font(Me.Font.FontFamily, 10)

        Dim iRow As Integer = 0
        For Each item As RTDValueInfo In _l
            Dim r As New Rectangle(left + 20, iRow * height + markTop + 1, 12, 12)
            Dim b As Brush = New SolidBrush(item.color)
            g.FillRectangle(b, r)
            Dim s As String
            s = item.Name
            g.DrawString(s, font, _foreBrush, left + 50, iRow * height + markTop)
            iRow += 1
        Next
    End Sub

    Private Sub DrawCoordinates(ByVal g As Graphics)
        Dim r As New Rectangle(aLeft, aTop, aWidth, aHeight)
        g.DrawRectangle(_forePen, r)
        'g.DrawLine(_forePen, aLeft, aTop + aHeight \ 2, aLeft + aWidth, aTop + aHeight \ 2)
        g.DrawLine(_forePen, aLeft + aWidth, aTop + aHeight, aLeft + aWidth + 15, aTop + aHeight)
        g.FillPolygon(New SolidBrush(_forePen.Color), New Point() {New Point(aLeft + aWidth + 15, aTop + aHeight), _
                                                                New Point(aLeft + aWidth + 5, aTop + aHeight + 3), _
                                                                New Point(aLeft + aWidth + 5, aTop + aHeight - 3) _
                                                    } _
                            )
    End Sub

    Private Sub DrawXCoordinate(ByVal g As Graphics, ByVal isPrint As Boolean)
        'If Me._dl.Count = 0 Then
        '    Exit Sub
        'End If

        'Dim index As Integer
        'index = Me._dl(0).CurrentIndex
        Dim tWidth As Single = 30
        Dim fb As Brush = _foreBrush
        If isPrint Then
            fb = _foreBrush4Print
        End If

        Dim dt As DateTime
        dt = Me.GetCurrentTime

        Dim p As New Pen(Brushes.Gray, 2)
        Dim p1 As New Pen(Brushes.DarkGray, 2)

        Dim sTime As String

        Dim dtT, dtF As DateTime
        Dim im As Decimal = 1
        Dim base, length As Integer
        base = 1

        dtT = dtpTo.Value
        dtF = dtpFrom.Value
        length = (dtT - dtF).TotalHours

        If length > 0 Then
            base = Math.Ceiling(length / 24)
            If base > 1 AndAlso base Mod 2 <> 0 Then base += 1
            im = 24 / length
            tWidth = 720 / length
        End If

        For i As Integer = 0 To length
            'If Math.Round(i Mod im) = 0 Then
            If (dtF.Hour + i) Mod base = 0 Then
                If i <> 0 AndAlso i <> length Then
                    sTime = ((dtF.Hour + i) Mod 24).ToString("D2") 'Math.Round(i / im)
                    g.DrawString(sTime, Me.Font, fb, GetX(i * tWidth) - 8, aTop + aHeight + 5)
                End If

                If ((dtF.Hour + i) Mod 24) = 0 Then
                    p1.Width = 2
                    g.DrawLine(p1, GetX(i * tWidth), aTop, GetX(i * tWidth), aTop + aHeight)
                Else
                    p.Width = 1
                    g.DrawLine(p, GetX(i * tWidth), aTop, GetX(i * tWidth), aTop + aHeight)
                End If
            End If

            If i = 0 Then
                'If (dtF.Hour Mod base) <= base / 2 Then
                sTime = ((dtF.Hour + i) Mod 24).ToString("D2") 'Math.Round(i / im)
                g.DrawString(sTime, Me.Font, fb, GetX(i * tWidth) - 15, aTop + aHeight + 5)
                'End If
                p.Width = 1
                g.DrawLine(p, GetX(i * tWidth), aTop, GetX(i * tWidth), aTop + aHeight)
                If (24 - dtF.Hour) * tWidth >= 90 Then
                    g.DrawString(dtF.ToLongDateString, Me.Font, fb, GetX(i * tWidth) - 50, aTop + aHeight + 20)
                End If
            ElseIf ((dtF.Hour + i) Mod 24) = 0 Then
                g.DrawString(dtF.AddDays(i / 24).ToLongDateString, Me.Font, fb, GetX(i * tWidth) - 50, aTop + aHeight + 20)
            End If

            If i = length Then
                p.Width = 1
                g.DrawLine(p, GetX(720), aTop, GetX(720), aTop + aHeight)
                sTime = ((dtF.Hour + i) Mod 24).ToString("D2") 'Math.Round(i / im)
                'If (dtF.Hour + i) Mod base = 0 Then
                '    g.DrawString(sTime, Me.Font, _foreBrush, GetX(720) - 8, aTop + aHeight + 5)
                'Else
                g.DrawString(sTime, Me.Font, fb, GetX(720), aTop + aHeight + 5)
                'End If
                g.DrawString(String.Format("小时(×{0})", base), Me.Font, Brushes.Gray, GetX(750), aTop + aHeight + 5)
            End If
        Next

        p.Dispose()
        p1.Dispose()

    End Sub

    Private Sub DrawYCoordinate(ByVal g As Graphics)
        Dim gap As Integer = 40

        Dim x As Integer = aLeft
        Dim iRow As Integer = 0
        Dim index As Integer = 0
        If hasTemp Then

            index += 1

            x -= gap
            Dim p As New Pen(Color.Orange)
            Dim b As New SolidBrush(Color.Orange)

            Dim yd As Integer = 0

            g.DrawLine(p, x, aTop + yd - 15, x, aTop + aHeight + yd)
            g.FillPolygon(New SolidBrush(p.Color), New Point() {New Point(x, aTop + yd - 15), _
                                                                New Point(x - 3, aTop + yd - 5), _
                                                                New Point(x + 3, aTop + yd - 5) _
                                                    } _
                            )
            g.DrawString("℃", Me.Font, b, x + 5, aTop + yd - 15)

            g.DrawLine(p, x, aTop + yd, x + gap, aTop + yd)
            g.DrawLine(p, x, aTop + aHeight + yd, x + gap, aTop + aHeight + yd)

            'g.DrawString("℃", Me.Font, New SolidBrush(p.Color), GetX(770), aTop - 15)

            'If index Mod 2 = 0 Then
            '    yd = -12
            'End If

            Dim kd As Integer = aHeight / SCALECOUNT
            Dim s As String
            For k As Integer = 0 To SCALECOUNT
                s = Math.Round(MAX_TEMP_LIMIT - k * (MAX_TEMP_LIMIT - MIN_TEMP_LIMIT) / SCALECOUNT, 2)
                g.DrawString(s, Me.Font, b, x, aTop + k * kd + yd)
                g.DrawLine(p, x - 2, aTop + k * kd, x + 2, aTop + k * kd)
            Next

            p.Dispose()
            b.Dispose()

        End If

        If hasHumi Then

            index += 1

            x -= gap
            Dim p As New Pen(Color.LightBlue)
            Dim b As New SolidBrush(Color.LightBlue)

            Dim yd As Integer = 0

            g.DrawLine(p, x, aTop + yd - 15, x, aTop + aHeight + yd)
            g.FillPolygon(New SolidBrush(p.Color), New Point() {New Point(x, aTop + yd - 15), _
                                                                New Point(x - 3, aTop + yd - 5), _
                                                                New Point(x + 3, aTop + yd - 5) _
                                                    } _
                            )
            g.DrawString("%", Me.Font, b, x + 5, aTop + yd - 15)

            g.DrawLine(p, x, aTop + yd, x + gap, aTop + yd)
            g.DrawLine(p, x, aTop + aHeight + yd, x + gap, aTop + aHeight + yd)

            'g.DrawString("％", Me.Font, New SolidBrush(p.Color), GetX(750), aTop - 15)

            'If index Mod 2 = 0 Then
            '    yd = -12
            'End If

            Dim kd As Integer = aHeight / SCALECOUNT
            Dim s As String
            For k As Integer = 0 To SCALECOUNT
                s = Math.Round(MAX_HUM_LIMIT - k * (MAX_HUM_LIMIT - MIN_HUM_LIMIT) / SCALECOUNT, 2)
                g.DrawString(s, Me.Font, b, x, aTop + k * kd + yd)
                g.DrawLine(p, x - 2, aTop + k * kd, x + 2, aTop + k * kd)
            Next

            p.Dispose()
            b.Dispose()

        End If

        Dim y As Integer = 0
        For i As Integer = 1 To SCALECOUNT - 1
            y += Me.aHeight / SCALECOUNT
            'g.DrawLine(Me._forePen, aLeft, aTop + y, aLeft + aWidth, aTop + y)
            g.DrawLine(Pens.Gray, x, aTop + y, aLeft + aWidth, aTop + y)
        Next

    End Sub

    Private Function GetX(ByVal x As Integer) As Integer
        Return Me.aLeft + x
    End Function

    'Public Function GetY3(ByVal y As Single, ByVal r As RTDValueInfo) As Integer
    '    Dim rn As New Random
    '    'y = rn.Next(1, 120)
    '    Dim t As Integer
    '    t = aHeight + aTop - ((y - r.minValue) / (r.maxValue - r.minValue)) * aHeight '- (r.maxValue + r.minValue) / 2
    '    Return t
    '    'Return 10 + y / 20
    'End Function

    Public Function GetY(ByVal y As Single, ByVal r As RTDValueInfo) As Integer
        Dim rn As New Random
        'y = rn.Next(1, 120)

        Dim t As Integer

        'todo:y值无效
        y = y Mod 20000

        Try
            t = aHeight + aTop - ((y - r.minValue) / (r.maxValue - r.minValue)) * aHeight '- (r.maxValue + r.minValue) / 2
        Catch ex As Exception
            Debug.WriteLine(ex.ToString)
            Throw ex
        End Try


        'Debug.WriteLine(r.Name & ": " & y & " - " & t)
        Return t
        'Return 10 + y / 20
    End Function


    Public Function GetY(ByVal y As Single) As Integer
        Return 10 + y / 20
    End Function

    Private Function GetCurrentTime() As DateTime
        Return dtpTo.Value
    End Function

    Private Sub DrawLine(ByVal g As Graphics)
        Dim SwitchCount As Integer = 0

        For Each d As RTData In _dl

            If d.rtdItem.length = 0 Then
                '开关量
                SwitchCount += 1
                Dim yy As Integer = Me.aTop + Me.aHeight - SwitchCount * 12

                Dim p As New Pen(d.rtdItem.color, 10)
                Dim p2 As New Pen(d.rtdItem.color, 1)
                For i As Integer = 1 To d.CurrentIndex
                    If d._bufByte(i) Mod 2 = 0 Then
                        g.DrawLine(p2, GetX(i - 1), yy, GetX(i), yy)
                    Else
                        g.DrawLine(p, GetX(i - 1), yy, GetX(i), yy)
                    End If
                Next
                p.Dispose()
                p2.Dispose()

            Else
                '曲线

                Dim p As New Pen(d.rtdItem.color)
                Dim pv, cv As Single
                'Dim ii As Integer = 0
                For i As Integer = 1 To d._bufSingle.Length - 1
                    If Not (Single.IsNaN(d._bufSingle(i - 1)) OrElse Single.IsNaN(d._bufSingle(i))) Then
                        'ii += 1
                        pv = d._bufSingle(i - 1)
                        cv = d._bufSingle(i)
                        'If pv >= d.rtdItem.minValue AndAlso pv <= d.rtdItem.maxValue Then
                        If pv <= d.rtdItem.maxValue Then
                            If pv >= d.rtdItem.minValue Then
                                If cv <= d.rtdItem.maxValue Then
                                    If cv >= d.rtdItem.minValue Then
                                        _g.DrawLine(p, GetX(i - 1), GetY(pv, d.rtdItem), GetX(i), GetY(cv, d.rtdItem))
                                    Else
                                        _g.DrawLine(p, GetX(i - 1), GetY(pv, d.rtdItem), GetX(i), GetY(Math.Max(cv, d.rtdItem.minValue), d.rtdItem))
                                    End If
                                Else
                                    _g.DrawLine(p, GetX(i - 1), GetY(pv, d.rtdItem), GetX(i), GetY(Math.Min(cv, d.rtdItem.maxValue), d.rtdItem))
                                End If
                            Else
                                If cv >= d.rtdItem.minValue Then
                                    _g.DrawLine(p, GetX(i - 1), GetY(Math.Max(pv, d.rtdItem.minValue), d.rtdItem), GetX(i), GetY(cv, d.rtdItem))
                                End If
                            End If
                        Else
                            If cv <= d.rtdItem.maxValue Then
                                If cv >= d.rtdItem.minValue Then
                                    _g.DrawLine(p, GetX(i - 1), GetY(Math.Min(pv, d.rtdItem.maxValue), d.rtdItem), GetX(i), GetY(cv, d.rtdItem))
                                Else
                                    _g.DrawLine(p, GetX(i - 1), GetY(Math.Min(pv, d.rtdItem.maxValue), d.rtdItem), GetX(i), GetY(Math.Max(cv, d.rtdItem.minValue), d.rtdItem))
                                End If
                            End If
                        End If
                        '_g.DrawLine(p, GetX(i - 1), GetY(d._bufSingle(i - 1), d.rtdItem), GetX(i), GetY(d._bufSingle(i), d.rtdItem))
                        'End If
                    End If
                Next
                p.Dispose()
            End If
        Next
    End Sub


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'If 1 = 1 Then
        '    Exit Sub
        'End If

        'Dim strSql As String

        'strSql = "Delete From tbccHDData"
        'SqlHelper.ExecuteNonQuery(strSql, Nothing)

        'Dim dt As DateTime = Now.ToLongDateString()

        'Dim tic As New TBCC.Utility.TranstrationItem()

        'For i As Integer = 0 To 2000
        '    strSql = "Insert Into tbccHDData(hdate,AI1,AI2,AI3,AI4,AI5,AI6,AI7,AI8,AI9,AI10,AI11,AI12,AI13,AI14,AI15,AI16,DO1,DO2,DO3,DO4,DO5,DO6) Values(?,?,?,?,?,  ?,?,?,?,?,  ?,?,?,?,?,  ?,?,1,1,1,  0,0,1) --" & i.ToString()

        '    Dim p(16) As SQLite.SQLiteParameter
        '    For j As Integer = 0 To p.Length - 1
        '        p(j) = New SQLite.SQLiteParameter()
        '    Next

        '    p(0).Value = UtilityClass.GetDateString(dt.AddMinutes(i * 2))

        '    For k As Integer = 1 To 16
        '        p(k).Value = g_rand.Next(20 - k, 50 + k * 2)
        '    Next

        '    tic.Add(strSql, p)
        'Next

        'SqlHelper.ExecuteTranstration(tic)


    End Sub

    Private Sub dtpFrom_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpFrom.ValueChanged

    End Sub


    Private Sub btnSavePic_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSavePic.Click
        Dim dlg As New SaveFileDialog
        dlg.Filter = "BMP (*.bmp;*.dib)|*.bmp;*.dib|" & _
                     "JPEG (*.JPG;*.JPEG;*.JPE;*.JFIF)|*.JPG;*.JPEG;*.JPE;*.JFIF|" & _
                     "GIF (*.GIF)|*.Gif|" & _
                     "TIFF (*.TIF;*.TIFF)|*.TIF;*.TIFF|" & _
                     "PNG (*.PNG)|*.Png"
        dlg.FileName = Now.ToString("yyyyMMdd") '& ".bmp"
        dlg.FilterIndex = 2
        If dlg.ShowDialog() = Windows.Forms.DialogResult.OK Then
            Dim format As System.Drawing.Imaging.ImageFormat
            Select Case dlg.FilterIndex
                Case 0
                    format = System.Drawing.Imaging.ImageFormat.Bmp
                Case 1
                    format = System.Drawing.Imaging.ImageFormat.Jpeg
                Case 2
                    format = System.Drawing.Imaging.ImageFormat.Gif
                Case 3
                    format = System.Drawing.Imaging.ImageFormat.Tiff
                Case 4
                    format = System.Drawing.Imaging.ImageFormat.Png
                Case Else
                    format = System.Drawing.Imaging.ImageFormat.Jpeg
            End Select

            RefreshImage4Print()
            Me._bmpPrint.Save(dlg.FileName, format)
            'Me.PictureBox1.Image.Save(dlg.FileName, System.Drawing.Imaging.ImageFormat.Bmp)
        End If
    End Sub


#Region "Print"

    Private Sub InitBG4Print(ByVal g As Graphics)
        g.Clear(Color.White)
        DrawMark4Print(g)
        DrawCoordinates4Print(g)
        DrawXCoordinate(g, True)
        DrawYCoordinate(g)
    End Sub

    ''' <summary>
    ''' 画曲线(打印用)
    ''' </summary>
    ''' <param name="g"></param>
    ''' <remarks></remarks>
    Private Sub DrawLine4Print(ByVal g As Graphics)
        Dim SwitchCount As Integer = 0
        For Each d As RTData In _dl
            If d.rtdItem.length = 0 Then
                '开关量
                SwitchCount += 1
                Dim yy As Integer = Me.aTop + Me.aHeight - SwitchCount * 12

                Dim p As New Pen(d.rtdItem.color, 10)
                Dim p2 As New Pen(d.rtdItem.color, 1)
                For i As Integer = 1 To d.CurrentIndex
                    If d._bufByte(i) Mod 2 = 0 Then
                        g.DrawLine(p2, GetX(i - 1), yy, GetX(i), yy)
                    Else
                        g.DrawLine(p, GetX(i - 1), yy, GetX(i), yy)
                    End If
                Next
                p.Dispose()
                p2.Dispose()

            Else
                '曲线

                Dim p As New Pen(d.rtdItem.color)
                Dim pv, cv As Single
                For i As Integer = 1 To d._bufSingle.Length - 1
                    ''CurrentIndex=0 ??????
                    'If Not (Single.IsNaN(d._bufSingle(i - 1)) OrElse Single.IsNaN(d._bufSingle(i))) Then
                    '    g.DrawLine(p, GetX(i - 1), GetY(d._bufSingle(i - 1), d.rtdItem), GetX(i), GetY(d._bufSingle(i), d.rtdItem))
                    'End If
                    If Not (Single.IsNaN(d._bufSingle(i - 1)) OrElse Single.IsNaN(d._bufSingle(i))) Then
                        'ii += 1
                        pv = d._bufSingle(i - 1)
                        cv = d._bufSingle(i)
                        'If pv >= d.rtdItem.minValue AndAlso pv <= d.rtdItem.maxValue Then
                        If pv <= d.rtdItem.maxValue Then
                            If pv >= d.rtdItem.minValue Then
                                If cv <= d.rtdItem.maxValue Then
                                    If cv >= d.rtdItem.minValue Then
                                        g.DrawLine(p, GetX(i - 1), GetY(pv, d.rtdItem), GetX(i), GetY(cv, d.rtdItem))
                                    Else
                                        g.DrawLine(p, GetX(i - 1), GetY(pv, d.rtdItem), GetX(i), GetY(Math.Max(cv, d.rtdItem.minValue), d.rtdItem))
                                    End If
                                Else
                                    g.DrawLine(p, GetX(i - 1), GetY(pv, d.rtdItem), GetX(i), GetY(Math.Min(cv, d.rtdItem.maxValue), d.rtdItem))
                                End If
                            Else
                                If cv >= d.rtdItem.minValue Then
                                    g.DrawLine(p, GetX(i - 1), GetY(Math.Max(pv, d.rtdItem.minValue), d.rtdItem), GetX(i), GetY(cv, d.rtdItem))
                                End If
                            End If
                        Else
                            If cv <= d.rtdItem.maxValue Then
                                If cv >= d.rtdItem.minValue Then
                                    g.DrawLine(p, GetX(i - 1), GetY(Math.Min(pv, d.rtdItem.maxValue), d.rtdItem), GetX(i), GetY(cv, d.rtdItem))
                                Else
                                    g.DrawLine(p, GetX(i - 1), GetY(Math.Min(pv, d.rtdItem.maxValue), d.rtdItem), GetX(i), GetY(Math.Max(cv, d.rtdItem.minValue), d.rtdItem))
                                End If
                            End If
                        End If
                        'g.DrawLine(p, GetX(i - 1), GetY(d._bufSingle(i - 1), d.rtdItem), GetX(i), GetY(d._bufSingle(i), d.rtdItem))
                        'End If
                    End If
                Next
                p.Dispose()
            End If
        Next
    End Sub

    Private Sub DrawCoordinates4Print(ByVal g As Graphics)
        Dim r As New Rectangle(aLeft, aTop, aWidth, aHeight)
        g.DrawRectangle(_forePen4Print, r)
        g.DrawLine(_forePen4Print, aLeft + aWidth, aTop + aHeight, aLeft + aWidth + 15, aTop + aHeight)
        g.FillPolygon(New SolidBrush(_forePen4Print.Color), New Point() {New Point(aLeft + aWidth + 15, aTop + aHeight), _
                                                                New Point(aLeft + aWidth + 5, aTop + aHeight + 3), _
                                                                New Point(aLeft + aWidth + 5, aTop + aHeight - 3) _
                                                    } _
                            )
    End Sub

    Private Sub DrawMark4Print(ByVal g As Graphics)
        Dim markTop As Integer = 25
        Dim height As Integer = 25
        Dim left As Integer = aLeft + aWidth + 40

        Dim rMark As New Rectangle(left, aTop, 120, height * _l.Count)
        g.DrawRectangle(_forePen4Print, rMark)

        Dim font As Font = New Font(Me.Font.FontFamily, 10)

        Dim iRow As Integer = 0
        For Each item As RTDValueInfo In _l
            Dim r As New Rectangle(left + 20, iRow * height + markTop + 1, 12, 12)
            Dim b As Brush = New SolidBrush(item.color)
            g.FillRectangle(b, r)
            Dim s As String
            s = item.Name
            g.DrawString(s, font, _foreBrush4Print, left + 50, iRow * height + markTop)
            iRow += 1
        Next
    End Sub
#End Region

    Private Delegate Sub VoidMethodHandler(ByVal obj As Object)
    Private Sub bwSearch_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bwSearch.DoWork
        Dim obj As Object = Nothing

        Dim str As String = String.Format("正在生成{0}历史曲线……", e.Argument(0))
        If Me.InvokeRequired Then
            Me.Invoke(New VoidMethodHandler(AddressOf SetSearchState), str)
        Else
            SetSearchState(str)
        End If

        RefreshData(e.Argument)

        If Me.InvokeRequired Then
            Me.Invoke(New VoidMethodHandler(AddressOf RefreshImageMethod), obj)
        Else
            RefreshImage()
        End If

    End Sub

    Private Sub bwSearch_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bwSearch.ProgressChanged
        Me.pbSearch.Value = e.ProgressPercentage
    End Sub

    Private Sub bwSearch_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bwSearch.RunWorkerCompleted
        Me.cboWarehouse.Enabled = True
        Me.dtpFrom.Enabled = True
        Me.dtpTo.Enabled = True
        Me.btnSavePic.Enabled = True
        Me.btnTHCCfg.Enabled = True
        Me.btnRefresh.Enabled = True
        Me.btnRefresh.Text = "刷新"
        Me.pnlSearch.Visible = False
        Me.PictureBox1.Focus()

        GC.Collect()
    End Sub
    Private Sub RefreshImageMethod(ByVal obj As Object)
        RefreshImage()
    End Sub
    Private Sub ReportProgress(ByVal value As Integer)
        If Not Me.IsDisposed Then
            If Me.InvokeRequired Then
                Me.Invoke(New VoidMethodHandler(AddressOf ReportProgressMethod), value)
            Else
                ReportProgressMethod(value)
            End If
        End If
    End Sub
    Private Sub ReportProgressMethod(ByVal obj As Object)
        Me.bwSearch.ReportProgress(obj)
    End Sub
    Private Sub SetSearchState(ByVal obj As Object)
        Me.lblSearchState.Text = obj
    End Sub

#Region "hiden"

    'Dim clickPoint, startPoint, endPoint As Point
    'Dim selectedSection As New Rectangle(0, 0, 0, 0)
    'Dim clickBmp As Bitmap
    'Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
    '    clickPoint = e.Location
    '    startPoint = clickPoint
    'End Sub

    'Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
    '    If clickPoint <> Nothing Then
    '        If e.Location.X > clickPoint.X Then
    '            If e.Location.Y > clickPoint.Y Then
    '                startPoint = clickPoint
    '                endPoint = e.Location
    '            Else
    '                startPoint = New Point(clickPoint.X, e.Y)
    '                endPoint = New Point(e.X, clickPoint.Y)
    '            End If
    '        Else
    '            If e.Location.Y > clickPoint.Y Then
    '                startPoint = New Point(e.X, clickPoint.Y)
    '                endPoint = New Point(clickPoint.X, e.Y)
    '            Else
    '                startPoint = e.Location
    '                endPoint = clickPoint
    '            End If
    '        End If

    '        selectedSection.Location = startPoint
    '        selectedSection.Size = Size_Abs(startPoint - endPoint)

    '        InitBG(_g)
    '        RefreshImage()
    '        _g.DrawRectangle(Pens.Yellow, selectedSection)

    '        Me.PictureBox1.Image = _bmp
    '    End If
    'End Sub

    'Private Sub PictureBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
    '    clickPoint = Nothing
    '    InitBG(_g)
    '    RefreshImage()
    '    Me.PictureBox1.Image = _bmp
    'End Sub

    'Private Function Size_Abs(ByVal size As Size) As Size
    '    size.Height = Math.Abs(size.Height)
    '    size.Width = Math.Abs(size.Width)

    '    Return size
    'End Function

#End Region

    Private Sub UpdateRTDValueInfo(ByVal dataType As Integer)
        For Each vi As RTDValueInfo In _l
            If vi.dataType = dataType Then
                Select Case dataType
                    Case 1
                        vi.minValue = MIN_TEMP_LIMIT
                        vi.maxValue = MAX_TEMP_LIMIT
                    Case 2
                        vi.minValue = MIN_HUM_LIMIT
                        vi.maxValue = MAX_HUM_LIMIT
                End Select
            End If
        Next
    End Sub

#Region "刻度线"

    Private tempBase, humiBase, timeBase As Single
    Private tempEnd, humiEnd As Integer
    Private timeStart As DateTime
    Private strLabCur As String
    Private strLabInfo As String
    Private curDateTime As DateTime
    Private curInfo(11) As Single

    Private Sub SetCurLabPara()
        tempEnd = MAX_TEMP_LIMIT
        humiEnd = MAX_HUM_LIMIT
        timeStart = dtpFrom.Value

        tempBase = (MAX_TEMP_LIMIT - MIN_TEMP_LIMIT) / (aHeight)
        humiBase = (MAX_HUM_LIMIT - MIN_HUM_LIMIT) / (aHeight)
        timeBase = (dtpTo.Value - dtpFrom.Value).TotalSeconds / (720)

        strLabCur = "日期：{0}" & vbCrLf
        strLabCur &= "时间：{1}" & vbCrLf

        If hasTemp Then
            strLabCur &= "温度：{2:F2} ℃" & vbCrLf
        End If

        If hasHumi Then
            strLabCur &= "湿度：{3:F2} %" & vbCrLf
        End If
    End Sub

    Private Sub SetInfoLabPara()
        Dim index As Integer = 2

        strLabInfo = "日期：{0}" & vbCrLf
        strLabInfo &= "时间：{1}" & vbCrLf

        For Each item As RTDValueInfo In _l
            Select Case item.dataType
                Case 1
                    strLabInfo &= item.Name & "：{" & index & ":F2} ℃" & vbCrLf
                Case 2
                    strLabInfo &= item.Name & "：{" & index & ":F2} %" & vbCrLf
            End Select
            index += 1
        Next
    End Sub

    Private Sub FreshCurInfo(ByVal i As Integer)
        Dim index As Integer = 0

        For Each item As RTData In _dl
            curInfo(index) = item._bufSingle(i)
            index += 1
        Next
    End Sub

    'mouse
    Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove

        If Me.pnlSearch.Visible = False Then
            If e.X >= aLeft AndAlso e.Y >= aTop AndAlso e.X <= aLeft + 720 AndAlso e.Y <= aTop + aHeight Then
                Dim curDT As DateTime
                Dim curDate, curTime As String

                curDT = timeStart.AddSeconds((e.X - aLeft) * timeBase)
                curDate = curDT.ToShortDateString()
                curTime = curDT.ToLongTimeString()

                '指针位置与信息
                labCur.Text = String.Format(strLabCur, _
                                            curDate, _
                                            curTime, _
                                            Math.Round(tempEnd - (e.Y - aTop) * tempBase, 2), _
                                            Math.Round(humiEnd - (e.Y - aTop) * humiBase, 2) _
                                            )
                labCur.Location = New Point(e.X + 15, e.Y + 15)
                labCur.Visible = True

                '刻度线信息
                If e.Button = Windows.Forms.MouseButtons.Left Then
                    keyIndex = e.X - aLeft
                    FreshCurInfo(keyIndex)
                    labInfo.Text = String.Format(strLabInfo, curDate, curTime, curInfo(0), curInfo(1), curInfo(2), curInfo(3), curInfo(4), curInfo(5), _
                                                                      curInfo(6), curInfo(7), curInfo(8), curInfo(9), curInfo(10), curInfo(11))
                    labLine.Location = New Point(e.X + Me.PictureBox1.Location.X, aTop + Me.PictureBox1.Location.Y)
                    labLine.Visible = True
                End If

                '刻度线位置
                If labLine.Visible = True Then
                    'labInfo.Location = New Point(labCur.Location.X, labCur.Location.Y + labCur.Size.Height + 5)
                    labInfo.Location = New Point(aLeft + 735, aTop + aHeight - labInfo.Size.Height)
                    labInfo.Visible = True
                End If

            Else
                labCur.Visible = False
                If e.Button = Windows.Forms.MouseButtons.Left Then
                    labLine.Visible = False
                    labInfo.Visible = False
                End If
            End If
        End If

    End Sub

    Private Sub PictureBox1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseClick
        Me.PictureBox1.Focus()

        If Me.pnlSearch.Visible = False Then
            If e.X >= aLeft AndAlso e.Y >= aTop AndAlso e.X <= aLeft + 720 AndAlso e.Y <= aTop + aHeight Then

                curDateTime = timeStart.AddSeconds((e.X - aLeft) * timeBase)

                '刻度线位置与信息
                keyIndex = e.X - aLeft
                FreshCurInfo(keyIndex)
                labInfo.Text = String.Format(strLabInfo, curDateTime.ToShortDateString, curDateTime.ToLongTimeString, _
                                                                      curInfo(0), curInfo(1), curInfo(2), curInfo(3), curInfo(4), curInfo(5), _
                                                                      curInfo(6), curInfo(7), curInfo(8), curInfo(9), curInfo(10), curInfo(11))
                labLine.Location = New Point(e.X + Me.PictureBox1.Location.X, aTop + Me.PictureBox1.Location.Y)
                'labInfo.Location = New Point(labCur.Location.X, labCur.Location.Y + labCur.Size.Height + 5)
                labInfo.Location = New Point(aLeft + 735, aTop + aHeight - labInfo.Size.Height)
                labLine.Visible = True
                labInfo.Visible = True

            Else
                labLine.Visible = False
                labInfo.Visible = False
            End If
        End If

    End Sub

    'keyboard
    Private keyIndex As Integer
    Private Sub PictureBox1_PreviewKeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PreviewKeyDownEventArgs) Handles PictureBox1.PreviewKeyDown
        Select Case e.KeyCode
            Case Keys.Left
                LabLine_Forward(1)

            Case Keys.Right
                LabLine_Backward(1)

            Case Keys.Up
                LabLine_Forward(5)

            Case Keys.Down
                LabLine_Backward(5)

            Case Keys.PageUp
                LabLine_Forward(10)

            Case Keys.PageDown
                LabLine_Backward(10)

            Case Keys.Home
                keyIndex = 0
                FreshLabInfo(keyIndex)
                labLine.Location = New Point(aLeft + keyIndex + Me.PictureBox1.Location.X, aTop + Me.PictureBox1.Location.Y)
                labInfo.Location = New Point(aLeft + 735, aTop + aHeight - labInfo.Size.Height)
                labLine.Visible = True
                labInfo.Visible = True

            Case Keys.End
                keyIndex = 720
                FreshLabInfo(keyIndex)
                labLine.Location = New Point(aLeft + keyIndex + Me.PictureBox1.Location.X, aTop + Me.PictureBox1.Location.Y)
                labInfo.Location = New Point(aLeft + 735, aTop + aHeight - labInfo.Size.Height)
                labLine.Visible = True
                labInfo.Visible = True

            Case Keys.Escape
                labLine.Visible = False
                labInfo.Visible = False

        End Select
    End Sub

    Private Sub FreshLabInfo(ByVal index As Integer)

        curDateTime = timeStart.AddSeconds((index) * timeBase)

        FreshCurInfo(index)
        labInfo.Text = String.Format(strLabInfo, curDateTime.ToShortDateString, curDateTime.ToLongTimeString, _
                                                 curInfo(0), curInfo(1), curInfo(2), curInfo(3), curInfo(4), curInfo(5), _
                                                 curInfo(6), curInfo(7), curInfo(8), curInfo(9), curInfo(10), curInfo(11))

    End Sub

    Private Sub LabLine_Forward(ByVal [step] As Integer)
        If labLine.Visible = False Then
            keyIndex = 720
            FreshLabInfo(keyIndex)
            labLine.Location = New Point(aLeft + keyIndex + Me.PictureBox1.Location.X, aTop + Me.PictureBox1.Location.Y)
            labInfo.Location = New Point(aLeft + 735, aTop + aHeight - labInfo.Size.Height)
            labLine.Visible = True
            labInfo.Visible = True
        Else
            If keyIndex > 0 Then
                keyIndex -= Math.Min([step], keyIndex)
                FreshLabInfo(keyIndex)
                labLine.Location = New Point(aLeft + keyIndex + Me.PictureBox1.Location.X, aTop + Me.PictureBox1.Location.Y)
            Else
                keyIndex = 720
                FreshLabInfo(keyIndex)
                labLine.Location = New Point(aLeft + keyIndex + Me.PictureBox1.Location.X, aTop + Me.PictureBox1.Location.Y)
            End If
        End If

    End Sub

    Private Sub LabLine_Backward(ByVal [step] As Integer)
        If labLine.Visible = False Then
            keyIndex = 0
            FreshLabInfo(keyIndex)
            labLine.Location = New Point(aLeft + keyIndex + Me.PictureBox1.Location.X, aTop + Me.PictureBox1.Location.Y)
            labInfo.Location = New Point(aLeft + 735, aTop + aHeight - labInfo.Size.Height)
            labLine.Visible = True
            labInfo.Visible = True
        Else
            If keyIndex < 720 Then
                keyIndex += Math.Min([step], (720 - keyIndex))
                FreshLabInfo(keyIndex)
                labLine.Location = New Point(aLeft + keyIndex + Me.PictureBox1.Location.X, aTop + Me.PictureBox1.Location.Y)
            Else
                keyIndex = 0
                FreshLabInfo(keyIndex)
                labLine.Location = New Point(aLeft + keyIndex + Me.PictureBox1.Location.X, aTop + Me.PictureBox1.Location.Y)
            End If
        End If
    End Sub

#End Region

    Private Sub PictureBox1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDoubleClick
        If (e.X >= _THC_Area.X AndAlso e.X <= (_THC_Area.X + _THC_Area.Width)) AndAlso (e.Y >= _THC_Area.Y AndAlso (e.Y <= _THC_Area.Y + _THC_Area.Height)) Then
            SetTHC()
        End If
    End Sub

    Private Sub btnTHCCfg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTHCCfg.Click
        SetTHC()
    End Sub

    Private Sub GetTHCArea()
        _THC_Area = New Rectangle(20, aTop, aLeft - 20, aHeight)
    End Sub

    Private Sub SetTHC()
        Dim frm As New HDCurveLimitCfg(cboWarehouse.SelectedItem)
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            cboWarehouse.SelectedItem.HDCurveTempUp = frm.RefTypeItem.HDCurveTempUp
            cboWarehouse.SelectedItem.HDCurveTempDown = frm.RefTypeItem.HDCurveTempDown
            cboWarehouse.SelectedItem.HDCurveHumiUp = frm.RefTypeItem.HDCurveHumiUp
            cboWarehouse.SelectedItem.HDCurveHumiDown = frm.RefTypeItem.HDCurveHumiDown
            MAX_TEMP_LIMIT = frm.RefTypeItem.HDCurveTempUp
            MIN_TEMP_LIMIT = frm.RefTypeItem.HDCurveTempDown
            MAX_HUM_LIMIT = frm.RefTypeItem.HDCurveHumiUp
            MIN_HUM_LIMIT = frm.RefTypeItem.HDCurveHumiDown

            UpdateRTDValueInfo(1)
            UpdateRTDValueInfo(2)

            InitBG(_g) '刷新图层但不显示数据
            Me.PictureBox1.Image = _bmp
        End If
    End Sub

End Class